package com.alix.paymoney.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alix.paymoney.R;
import com.alix.paymoney.base.App;
import com.alix.paymoney.base.BaseActivity;
import com.alix.paymoney.presenter.VerifyInfoPresneter;
import com.alix.paymoney.presenter.impl.VerifyInfoPresenterImpl;
import com.alix.paymoney.view.VerifyInfoView;
import com.bigkoo.pickerview.OptionsPickerView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VerifyActivity extends BaseActivity implements VerifyInfoView {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_cert)
    EditText etCert;
    @BindView(R.id.education)
    TextView education;
    @BindView(R.id.verify)
    TextView verify;

    String name, certNum, eduNum;
    private OptionsPickerView pickerView;
    private VerifyInfoPresneter verifyInfoPresneter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_verify;
    }

    @Override
    protected void afterCreate(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        verifyInfoPresneter = new VerifyInfoPresenterImpl(this);
    }

    @OnClick({R.id.back, R.id.education, R.id.verify})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.education:
                showPickerView();
                break;
            case R.id.verify:
                name = etName.getText().toString().trim();
                certNum = etCert.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    showTips("请输入姓名");
                    return;
                }

                if (TextUtils.isEmpty(certNum)) {
                    showTips("请输入身份证号码");
                    return;
                }
                if (certNum.length() != 18) {
                    showTips("请输入18位身份证号码");
                }
                if (TextUtils.isEmpty(eduNum)) {
                    showTips("请选择学历");
                    return;
                }
                verifyInfoPresneter.verifyInfo(App.getToken(), name, certNum, eduNum);

                break;
        }
    }

    private void showPickerView() {
        pickerView = new OptionsPickerView(this);
        final ArrayList<String> list = new ArrayList<>();
        pickerView.setTitle("请选择学历");
        list.add("其他");
        list.add("小学");
        list.add("初中");
        list.add("高中");
        list.add("专科");
        list.add("本科");
        list.add("研究生");
        pickerView.setPicker(list);
        pickerView.setSelectOptions(5);
        pickerView.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3) {
                eduNum = list.get(options1);
            }
        });
        pickerView.show();
    }

    @Override
    public void onBackPressed() {

        if (pickerView != null && pickerView.isShowing()) {
            pickerView.dismiss();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void verifySuccess() {

    }

    @Override
    public void vefiryError(int errorCode, String errorInfo) {

    }

    @Override
    public void showTips(String msg) {
        showSnackBar(back, msg);
    }
}
